﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using com.nm.library.Helper;
using InventoryToPredictAPI.model;
using com.nm;

namespace InventoryToPredictAPI.Export
{
    [Export(typeof(ExcelImport))]
    public class NdwKnockMatExcel : ExcelImport
    {
        public override ExcelImportType Type => ExcelImportType.NDWKNOCKMATSET;
        public override Dictionary<string, ImportVerify> DictFields => dictFields;
        #region Excel字段映射
        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="货号",FieldName="MaterialID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)}
          }.ToDictionary(e => e.ColumnName, e => e);
        #endregion


        //NDWKNOCKMATSET

        public override DataTable GetExportData(string Keys)
        {
            DataTable dt = new DataTable();
            return dt;
        }

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = 0;
            DataTable msg = new DataTable();
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            {
                var handler = new NdwKnockMatHandler();
                var list = ModelConvertHelper<NdwKnockMatModel.ListModel>.ConvertToModel(dt);
                handler.ImportData(list, out msg, out index);
                result.Message = $"成功导入{index}条数据{(dt.Rows.Count - index == 0 ? "" : $@",有{dt.Rows.Count - index}条错误数据")}";

                //if (msg.Rows.Count > 0)
                //{
                //    var path = ExcelImporMapper.GetTemplatePath(Type);
                //    result.Url = ExcelUtil.ExportErrorExeclToDB(msg, path, dictFields, "淘汰品设置", index);
                //}


            }
            catch (Exception ex)
            {
                Log.Error("导入淘汰品货号：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            return result;

        }
    }
}
